import { parseTime, request } from "../../utils/util";
let app = getApp()

Page({
  data: {
    userInfo: app.globalData.userInfo,
    details: {},
    comment: [],
    commentFocus: false,
    form: {
      content: '',
      activity_id: '',
      comment_id: '',
    }
  },
  onInputChange(e) {
    const { value } = e.detail;
    const { name } = e.currentTarget.dataset;
    const { form } = this.data;
    this.setData({
      form: { ...form, [name]: value }
    });
  },
  // 获取数据
  getDetail() {
    wx.showLoading({
      title: '加载中..',
      mask: true
    })
    request({
      url: `/api/web/activity/detail/${this.options.id}`,
      success: ({ code, data }) => {
        wx.hideLoading()
        if (code === 200) {
          data.start_date_show = parseTime(data.start_date, '{m}月{d}日')
          data.end_date_show = parseTime(data.end_date, '{m}月{d}日')
          this.setData({
            details: data
          })
        }
      }
    })
  },
  // 获取评价数据
  getComment() {
    wx.showLoading({
      title: '加载中..',
      mask: true
    })
    request({
      url: `/api/web/activity/comment`,
      params: {
        id: this.options.id
      },
      success: ({ code, data }) => {
        wx.hideLoading()
        if (code === 200) {
          this.setData({
            comment: data
          })
        }
      }
    })
  },
  previewImg(ev) {
    let { item } = ev.currentTarget.dataset;
    wx.previewImage({
      current: item, // 当前显示图片的http链接
      urls: this.data.details.img // 需要预览的图片http链接列表
    })
  },
  revertComment(e) {
    if (!app.globalData.userInfo.nickname) {
      wx.showToast({
        title: '请先登录',
        icon: 'none',
        duration: 2000
      })
      setTimeout(() => {
        wx.switchTab({
          url: '/pages/mine/index'
        })
      }, 1000)
      return false
    } else if (!app.globalData.userInfo.telephone) {
      wx.showToast({
        title: '请绑定手机号',
        icon: 'none',
        duration: 2000
      })
      setTimeout(() => {
        wx.navigateTo({
          url: '/pages/login/index'
        })
      }, 1000)
      return false
    }
    const { item } = e.currentTarget.dataset;
    this.setData({
      commentFocus: true,
      commentPlaceholder: `回复${item.nickname}`,
      form: {
        ...this.data.form,
        comment_id: item.id
      }
    })
  },
  commentBlur() {
    if (!this.data.form.content) {
      this.setData({
        commentPlaceholder: '',
        form: {
          ...this.data.form,
          comment_id: ''
        }
      })
    }
  },
  save() {
    this.data.form.activity_id = this.options.id;
    wx.showLoading({
      title: '加载中..',
      mask: true
    })
    request({
      url: '/api/web/activity/comment',
      method: 'POST',
      data: this.data.form,
      success: ({ code, message }) => {
        wx.hideLoading()
        if (code === 200) {
          this.setData({
            commentPlaceholder: '',
            form: {
              ...this.data.form,
              comment_id: '',
              content: ''
            }
          })
          this.getComment();
        } else {
          wx.showToast({
            title: message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  onLoad () {
    this.getDetail();
    this.getComment();
  },
  onShow () {
    wx.setNavigationBarColor({
      frontColor: '#000000',
      backgroundColor: '#000000',
      animation: {
        duration: 400,
        timingFunc: 'easeIn'
      }
    })
  }
})
